package org.apache.zookeeper.server.paxos2;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 * User: wangw
 * Date: 2011-6-12
 * Time: 21:41:48
 * To change this template use File | Settings | File Templates.
 */
public class StartSyncResponse extends Message {

    public boolean last = false;
    public int sid = 0;
	public long zxid;
    public long[] blocks;
    public byte[][] blockMetas;
    
    @Override
	public void deserialize(DataInputStream in) throws IOException {
        last = in.readBoolean();
        sid = in.readInt();
    	zxid = in.readLong();
    	int size = in.readInt();
    	blocks = new long[size];
    	for(int i=0; i<size; i++)
    		blocks[i] = in.readLong();
    	blockMetas = new byte[size][];
    	for(int i=0; i<size; i++){
    		int tmp = in.readInt();
    		if(tmp==0)
    			blockMetas[i] = null;
    		else{
    			blockMetas[i] = new byte[tmp];
    			in.readFully(blockMetas[i]);
    		}
    	}
		
	}
	@Override
	public void serialize(DataOutputStream out) throws IOException {
		assert blocks.length == blockMetas.length;
        out.writeBoolean(last);
        out.writeInt(sid);
		out.writeLong(zxid);
		out.writeInt(blocks.length);
		for(int i=0; i<blocks.length; i++)
			out.writeLong(blocks[i]);
		for(int i=0; i<blockMetas.length; i++){
			if(blockMetas[i]==null)
				out.writeInt(0);
			else{
				out.writeInt(blockMetas[i].length);
				out.write(blockMetas[i]);
			}
		}
			
		
		
	}

}
